//
//  PicView.m
//  UI06_UIScorollView_UIPageControl
//
//  Created by dingcong on 16/5/10.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PicView.h"
#import "PictureScrollView.h"
@interface PicView()<UIScrollViewDelegate>
@property(nonatomic, retain)UIPageControl *page;
@property(nonatomic, retain)PictureScrollView *picScroll;

@end

@implementation PicView


- (void)dealloc{
    
    [_page release];
    [_picScroll release];
    [super dealloc];
    
    
}

#pragma mark - override

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self)
    {
        //初始化子控件
        self.picScroll = [[PictureScrollView alloc]initWithFrame:self.bounds];
        [self addSubview:self.picScroll];
        [_picScroll release];
        
        
        self.picScroll.delegate = self;
        
        self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetWidth(self.bounds) - 40, CGRectGetWidth(self.bounds), 40 )];
        
        [self addSubview:self.page];
        [_page release];
        [self.page addTarget:self action:@selector(handlePage:) forControlEvents:UIControlEventValueChanged];
        
        
    }
    return self;
}

#pragma mark - scrollView Delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    //设置page.currentPage属性
    self.page.currentPage = scrollView.contentOffset.x / CGRectGetWidth(self.bounds);
    
    
    
}

#pragma mark - page
- (void)handlePage:(UIPageControl *)page
{
    
    //设置scoll.contentOffSet偏移量
    [self.picScroll setContentOffset:CGPointMake(page.currentPage * CGRectGetWidth(self.bounds), 0)animated:YES];
    
    
    
}

#pragma mark -
- (void)addPictures:(NSArray <UIImage *>*)pictures
{
    
    [self.picScroll addPictures:pictures];
    
    self.page.numberOfPages = pictures.count;
    
}


@end
